#------------------------------------------------------------------------------
# Import libraries
#------------------------------------------------------------------------------

rm(list=ls())
library(LalRUtils)
libreq(tidyverse, data.table, zoo, tictoc, fst, fixest, PanelMatch, patchwork,
       rio, magrittr, janitor, did, panelView, ggiplot, tictoc, binsreg, interflex)
set.seed(42)
theme_set(lal_plot_theme())

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Define Paths
#------------------------------------------------------------------------------

# R studio
setwd( dirname(rstudioapi::getActiveDocumentContext()$path) )
# R default : unccoment if you use default R
# setwd(getSrcDirectory(function(){})[1])
#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Load Data
#------------------------------------------------------------------------------

vcf <- fread("vcf_data_complete.csv", sep = ",")
setnames( vcf, "d", "D")
vcf_data <- copy( vcf )
#------------------------------------------------------------------------------




#------------------------------------------------------------------------------
# Table A2: Summary Statistics for primary analysis sample (VCF Data) - 
#           above median forest cover in 1990
#------------------------------------------------------------------------------

# Import data

library(vtable)
# %% summary table - VCF
sumvars = c('forest_index', 'green_index', 'built_index', 'sch', 'cover_1990')
summvars = c('notNA(x)', 'mean(x)', 'sd(x)', 'min(x)', 'pctile(x)[25]', 'median(x)', 'pctile(x)[75]', 'max(x)')
labs = c(
  'Forest cover index (0-100)',
  'Non-forest green index (0-100)',
  'Non-green index (0-100)',
  'Scheduled Status',
  'Forest Cover in 1990 (Ex-Ante)'
)

sumtable(vcf_data[cover_1990 > quantile(cover_1990, 0.5), ..sumvars],
         factor.percent = FALSE, digits=5,
         factor.counts = FALSE, summ = summvars,
         labels = labs, 
         title = "Summary Statistics for primary analysis 
         sample (VCF Data) - above median forest cover in 1990",
         file = "appendix_tableA2.tex", out = 'latex')

#------------------------------------------------------------------------------
